package com.jwdf.uda.ui.activity.presenter;

import android.os.Handler;
import android.util.Log;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.bean.UploadImageResponse;
import com.jwdf.uda.event.NewProblemsEvent;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.UploadImageAction;
import com.jwdf.uda.ui.activity.view.INewVoteView;
import com.jwdf.uda.ui.fragment.question.bean.NewVote;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.utils.ToastUtils;
import com.jwdf.uda.utils.UdaUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：
 */
public class NewVotePresenter extends APresenter<INewVoteView> {
    public final static int PLAN_A = 1;
    public final static int PLAN_B = 2;
    public final static int PLAN_C = 3;
    public final static int PLAN_D = 4;

    public NewVotePresenter(INewVoteView iView) {
        super(iView);
    }

    private List<UploadImage> getUploadImageList(int plan) {
        return mIView.getUploadImageList(plan);
    }
    private List<NewVote> getNewVoteList(){
        return mIView.getNewVoteList();
    }
    private String  getDescription(int plan){
        return mIView.getDescription(plan);
    }

    public void onHeadActionClick() {
        if(!mIView.isVoteBVisibility()){
            ToastUtils.show(getContext(), R.string.vote_option);
            return;
        }
        if (getDescription(PLAN_A).trim().length()>=100 || getDescription(PLAN_A).trim().length()==0) {
            ToastUtils.show(getContext(), R.string.vote_scheme_a);
            return;
        }
        if (getDescription(PLAN_B).trim().length()>=100 || getDescription(PLAN_B).trim().length()==0) {
            ToastUtils.show(getContext(), R.string.vote_scheme_b);
            return;
        }
        if (mIView.isVoteCVisibility() && (getDescription(PLAN_C).trim().length()>=100 || getDescription(PLAN_C).trim().length()==0)) {
            ToastUtils.show(getContext(), R.string.vote_scheme_c);
            return;
        }
        if (mIView.isVoteDVisibility() && (getDescription(PLAN_D).trim().length()>=100 || getDescription(PLAN_D).trim().length()==0)) {
            ToastUtils.show(getContext(), R.string.vote_scheme_d);
            return;
        }

        mIView.getFragment().showProgressDialog();
        new Handler().postDelayed(new Runnable() {
            public void run() {
                // do something
                doUploadAImagesAction();
            }

        }, 100);

    }

    protected void doUploadAImagesAction() {
        boolean hasImageAToken = doUploadImagesAction(PLAN_A);
        boolean hasImageBToken = doUploadImagesAction(PLAN_B);
        boolean hasImageCToken = doUploadImagesAction(PLAN_C);
        boolean hasImageDToken = doUploadImagesAction(PLAN_D);
        if (hasImageAToken && hasImageBToken && hasImageCToken && hasImageDToken) {
            doHasImageTokenSuccess();
        }
    }

    private boolean doUploadImagesAction(int plan) {
        List<UploadImage> uploadImages = getUploadImageList(plan);
        if (uploadImages == null) return true;
        boolean hasImageToken = true;
        for (int i = 0; i < uploadImages.size(); i++) {
            UploadImage image = uploadImages.get(i);
            if (StringUtils.isNullEmpty(image.getImageToken())) {
                hasImageToken = false;
                doUploadImageAction(plan, image.getImageUrl());
            }
        }
        return hasImageToken;
    }

    private void doUploadImageAction(final int plan, final String imageUrl) {
        UploadImageAction action = new UploadImageAction(getContext(), imageUrl);
        mIView.getFragment().onNewRequestCall(action.request(new UploadImageAction.IResCallback<String>() {
            @Override
            public void onCallback(int resultCode, UploadImageResponse response) {
                if (mIView.getFragment().getView() != null) {
                    if (HttpHelper.showError(getContext(), resultCode, response, getContext().getString(R.string.error_operation))) {
                        mIView.getFragment().cancelRequest();
                        mIView.getFragment().closeProgressDialog();
                        return;
                    }
                    doUploadImageResponse(plan, imageUrl, response);
                }
            }
        }));


    }

    private void doUploadImageResponse(int plan, String imageUrl, UploadImageResponse response) {
        mIView.setImageToken(plan, new UploadImage(imageUrl, response.getToken()));

        doHasImageTokenSuccess();
    }

    //判断是否图片已经上传完成
    private void doHasImageTokenSuccess() {
        Log.i("-------",""+",,,");
        List<UploadImage> uploadAImages = getUploadImageList(PLAN_A);
        List<UploadImage> uploadBImages = getUploadImageList(PLAN_B);
        List<UploadImage> uploadCImages = getUploadImageList(PLAN_C);
        List<UploadImage> uploadDImages = getUploadImageList(PLAN_D);
        List<String> imageATokens = getImageTokenList(PLAN_A);
        List<String> imageBTokens = getImageTokenList(PLAN_B);
        List<String> imageCTokens = getImageTokenList(PLAN_C);
        List<String> imageDTokens = getImageTokenList(PLAN_D);
        if(uploadAImages != null && uploadAImages.size() >0){
            if(imageATokens !=null && imageATokens.size()>0){
                if(uploadAImages.size()==imageATokens.size()){

                }else {
                    error();
                    return;
                }
            }else {
                return;
            }
        }
        if(uploadBImages != null && uploadBImages.size() >0){
            if(imageBTokens !=null && imageBTokens.size()>0){
                if(uploadBImages.size()==imageBTokens.size()){

                }else {
                    error();
                    return;
                }
            }else {
                return;
            }
        }
        if(uploadCImages != null && uploadCImages.size() >0){
            if(imageCTokens !=null && imageCTokens.size()>0){
                if(uploadCImages.size()==imageCTokens.size()){

                }else {
                    error();
                    return;
                }
            }else {
                return;
            }
        }
        if(uploadDImages != null && uploadDImages.size() >0){
            if(imageDTokens !=null && imageDTokens.size()>0){
                if(uploadDImages.size()==imageDTokens.size()){

                }else {
                    error();
                    return;
                }
            }else {
                return;
            }
        }
        doVoteAddAction();
    }

    public void error(){
        ToastUtils.show(getContext(), R.string.error_net);
    }

    protected List<String> getImageTokenList(int plan) {
        List<String> imageTokens = new ArrayList<>();
        List<UploadImage> uploadImages = getUploadImageList(plan);
        if (uploadImages == null) return imageTokens;
        for (int i = 0; i < uploadImages.size(); i++) {
            UploadImage image = uploadImages.get(i);
            if (!StringUtils.isNullEmpty(image.getImageToken())) {
                imageTokens.add(image.getImageToken());
            } else {
                return null;
            }
        }
        return imageTokens;
    }

    private void doVoteAddAction() {
        List<NewVote> list=getNewVoteList();
        EventBusUtil.post(new NewProblemsEvent(NewProblemsEvent.EVENT_COMMENT_VOTE,list));
        UdaUtils.hideInputMethod(mIView.getFragment().getContext(), mIView.getFragment().getView());
        mIView.getFragment().getActivity().finish();
    }

}
